package pers.mor.tomcat.catalina;

import cn.hutool.core.date.TimeInterval;
import cn.hutool.log.LogFactory;

import java.util.ArrayList;
import java.util.List;

public class Service implements Runnable {

    private String name;
    private Engine engine;
    private List<Connector> connectorList;
    private List<Integer> portList;

    public Service(String name, Engine engine) {
        this.name = name;
        this.engine = engine;
        this.connectorList = new ArrayList<>();
        this.portList = new ArrayList<>();
    }

    public void start() {
        new Thread(this).start();
    }

    @Override
    public void run() {
        this.init();
    }

    public void init() {
        TimeInterval interval = new TimeInterval();
        interval.start();
        for (Connector c : this.connectorList) {
            c.init();
        }

        LogFactory.get().info("Initialization processed in {} ms", interval.intervalMs());

        for (Connector c : this.connectorList) {
            c.start();
        }
        // 用于日志显示
        Server.serviceNum(1);
        Server.elapsedTime(interval);
    }

    public String getName() {
        return name;
    }

    public Engine getEngine() {
        return engine;
    }

    public List<Connector> getConnectorList() {
        return connectorList;
    }

    public List<Integer> getPortList() {
        return portList;
    }
}
